package com.eascs.web;

import java.util.EnumSet;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.SessionTrackingMode;

import org.apache.commons.codec.CharEncoding;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.eascs.site.session.SessionFilter;
import com.eascs.web.context.ContextFilter;

public abstract class AbstractApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

	@Override
	protected Filter[] getServletFilters() {
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding(CharEncoding.UTF_8);
		characterEncodingFilter.setForceEncoding(true);
		return new Filter[] { characterEncodingFilter, new ContextFilter(),new SessionFilter() };
	}

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		//为了取消url-track 跳转时 会自动添加jsssionid 
		servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));
		super.onStartup(servletContext);
	}

	@Override
	protected void customizeRegistration(Dynamic registration) {
		registration.setInitParameter("throwExceptionIfNoHandlerFound","true");
		super.customizeRegistration(registration);
	}

	@Override
	protected javax.servlet.FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
		return super.registerServletFilter(servletContext, filter);
	}

}
